<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
       <el-table-column label="采购订单号" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.displayPoNumber`" :rules="formRules.displayPoNumber" class="mb-0px!">
            {{ row.displayPoNumber }}
          </el-form-item>
        </template>
      </el-table-column>
<!--      <el-table-column label="采购订单明细行" min-width="150">-->
<!--        <template #default="{ row, $index }">-->
<!--          <el-form-item :prop="`${$index}.poId`" :rules="formRules.poId" class="mb-0px!">-->
<!--            <el-input v-model="row.poId" placeholder="请输入采购订单明细行" />-->
<!--          </el-form-item>-->
<!--        </template>-->
<!--      </el-table-column>-->

      <el-table-column label="物料号" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.itemCode`"  class="mb-0px!">
            {{ row.itemCode }}
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="物料名" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.itemName`"  class="mb-0px!">
            {{ row.itemName }}
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="送货数量" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.qty`"  class="mb-0px!">
            {{ row.qty }}
          </el-form-item>
        </template>
      </el-table-column>

      <el-table-column label="采购订单行号" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.qty`"  class="mb-0px!">
            {{ row.lineNum }}
          </el-form-item>
        </template>
      </el-table-column>

      <el-table-column label="项目代码" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.qty`"  class="mb-0px!">
            {{ row.projectCode }}
          </el-form-item>
        </template>
      </el-table-column>

      <el-table-column label="项目名称" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.qty`"  class="mb-0px!">
            {{ row.projectName }}
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <el-button @click="handleAddOrderDetailForm" round>+ 添加送货单明细</el-button>
  </el-row>

  <selectOrderDetailForm ref="orderDetailFormRef" @onGetChild="getChild"  STYLE="width: 1400px"/>

</template>
<script setup lang="ts">
import { OrderDeliveryApi } from '@/api/ck/orderdelivery'
import SelectOrderDetailForm from "@/views/ck/purorder/components/SelectOrderDetailForm.vue";

const props = defineProps<{
  mainId: undefined // 送货单ID（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formDataOrderDetailIds = ref([]) // 字数据IDS
const formRules = reactive({
  mainId: [{ required: true, message: '送货单ID不能为空', trigger: 'blur' }],
  displayPoNumber: [{ required: true, message: '采购订单号不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref
const orderDetailFormRef = ref() // 表单 Ref
/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.mainId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      formData.value = await OrderDeliveryApi.getOrderDeliveryDetailListByMainId(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    mainId: undefined,
    displayPoNumber: undefined,
    poId: undefined,
    qty: undefined,
    status: undefined,
    itemCode: undefined,
    itemName: undefined,
    lineNum: undefined,
    projectCode: undefined,
    projectName: undefined,
    vendorCode: undefined,
    vendorName: undefined,
  }
  row.mainId = props.mainId
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}


const handleAddOrderDetailForm= () => {
  orderDetailFormRef.value.open()
}


/** 表单值 */
const getData = () => {
  return formData.value
}

const getChild = (childValue) => {
  console.log(formDataOrderDetailIds.value)
  if (childValue.length > 0) {
    for (let i = 0; i < childValue.length; i++) {

      if(formDataOrderDetailIds.value.indexOf(childValue[i].id) === -1){

        formDataOrderDetailIds.value.push(childValue[i].id)

        const row = {
          id: undefined,

          poId: childValue[i].id,
          displayPoNumber: childValue[i].displayPoNumber,
          qty: childValue[i].quantity,
          itemCode: childValue[i].itemCode,
          itemName: childValue[i].itemDescription,
          lineNum: childValue[i].lineNum,
          projectCode: childValue[i].projectCode,
          projectName: childValue[i].projectName,
          vendorCode: childValue[i].vendorCode,
          vendorName: childValue[i].vendorName,
        }
        row.mainId = props.mainId
        formData.value.push(row)

      }

    }

  }
}





defineExpose({ validate, getData })
</script>
